package com.yuneec.fly.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.yuneec.fly.R;


public class KnobsView extends View {
    private int degree = 0;

    private Drawable mKnobDrawable;
    private Drawable mKnobBackground;
    private int mCenterX;
    private int mCenterY;

    public KnobsView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public KnobsView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KnobsView(Context context) {
        this(context, null);
    }

    private void init(Context context) {
        mKnobDrawable = context.getResources().getDrawable(R.drawable.system_setting_hardware_monitor_knobs);
        mKnobDrawable.setBounds(0, 0, mKnobDrawable.getIntrinsicWidth(), mKnobDrawable.getIntrinsicHeight());
        mKnobBackground = context.getResources().getDrawable(R.drawable.system_setting_hardware_monitor_knobs_back_ground);
        mKnobBackground.setBounds(0, 0, mKnobBackground.getIntrinsicWidth(), mKnobBackground.getIntrinsicHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mKnobBackground.draw(canvas);

        canvas.save();
        canvas.rotate(degree, mCenterX, mCenterY);
        mKnobDrawable.draw(canvas);
        canvas.restore();
    }

    public void setValue(int value) {
        if (degree != value) {
            degree = value;
            invalidate();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mKnobDrawable.getIntrinsicWidth(), mKnobDrawable.getIntrinsicHeight());
        mCenterX = (mKnobDrawable.getIntrinsicWidth() >> 1) + 1;
        mCenterY = (mKnobDrawable.getIntrinsicHeight() >> 1) + 1;
    }
}
